/* eslint-disable import/prefer-default-export */
import Taro from '@tarojs/taro'
export const isType = (type) => {
    return (o) => {
        return Object.prototype.toString.call(o) === `[object ${type}]`
    }
}
export const isObject = isType('Object');
export const isArray = isType('Array');
export const isNumber = isType('Number');
export const equalObject = (a, b) => {
    if (isObject(a) != isObject(a)) {
        return false;
    }
    for (let v in b) {
        if (a[v] != b[v]) {
            return false;
        }
    }
    return true;
}
export function serialize(data) {
    let p = [];

    data && Object.keys(data).forEach((key) => {
        if (data[key] !== undefined && data[key] !== null) {
            p.push(`${key}=${data[key]}`)
        }

    })
    return p.join('&')
}
const key_history = 'local_footprint'
const key_favorite = 'local_favorite';
const MAX_SAVE_FOOTPRINT = 30;
export const findArrayIndex = (arr, o) => {
    if (!isObject(o) && !isArray(o)) {
        return arr.indexOf(o);
    }
    for (let i = 0; i < arr.length; i++) {
        if (equalObject(arr[i], o)) {
            return i;
        }
    }
    return -1;
}
export function queryHistory() {
    return Taro.getStorageSync(key_history) || []
}
export function queryFavorite() {
    return Taro.getStorageSync(key_favorite) || []
}
export function findFavoriteIndex(item) {
    const list = queryFavorite()
    return list.findIndex(value => value.link == item.link);
}
export function removeFavorite(item) {
    const list = queryFavorite()
    let index = findFavoriteIndex(item);
    if (index != -1) {
        list.splice(index, 1);
    }

    Taro.setStorageSync(key_favorite, list)
}
export function pushFavorite(item) {
    const list = queryFavorite()
    //    let footprint: Array<any> = getFootprint();
    let index = findFavoriteIndex(item);
    if (index != -1) {
        list.splice(index, 1);
    }
    // if (his.length > MAX_SAVE_FOOTPRINT) {
    //     his.pop();
    // }
    list.unshift(item);
    Taro.setStorage({
        key: key_favorite,
        data: list
    })
}
//添加历史
export function pushHistory(item) {
    const list = queryHistory()
    //    let footprint: Array<any> = getFootprint();
    let index = list.findIndex(value => value.link == item.link);
    if (index > -1) {
        list.splice(index, 1);
    }
    if (list.length > MAX_SAVE_FOOTPRINT) {
        list.pop();
    }
    list.unshift(item);
    // console.log(list,'list'+key_history)
    // Taro.setStorageSync(key_history, list)
    Taro.setStorage({
        key: key_history,
        data: list
    })
}

export function dateFormat(date ,fmt='YYYY-mm-dd HH:MM:SS') {
    if(!date){
        return  ''
    }
    date = new Date(date)
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}